/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Phong
 */
public class NguoiDungDAO {
      public static boolean Them(NguoiDung ND) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO nguoi_dung(ID, ho_ten, so_cmnd,ngay_sinh,gioi_tinh,dia_chi,dien_thoai,loai_nguoi_dung,ngay_tao) VALUES('%d','%s','%s','%s','%s','%s','%s','%d','%s')",ND.Id,ND.HoTen,ND.CMND,ND.NgaySinh,ND.GioiTinh,ND.DiaChi,ND.DienThoai,ND.LoaiNguoiDung,ND.NgayTao );
            int n = statement.executeUpdate(sql);
            conn.close();
            return true;
           //JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);

        } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex.getMessage(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
      public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from nguoi_dung where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
          public static boolean Update(NguoiDung NV)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update nguoi_dung set loai_nguoi_dung = ?, dien_thoai = ?, dia_chi = ?, gioi_tinh=?, "
                    + "ngay_sinh=?, so_cmnd=?, ho_ten=? Where id = ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, NV.LoaiNguoiDung);
            ps.setString(2, NV.DienThoai);
            ps.setString(3, NV.DiaChi);
            ps.setString(4, NV.GioiTinh);
            ps.setString(5, NV.NgaySinh);
            ps.setString(6, NV.CMND);
            ps.setString(7, NV.HoTen);
            ps.setInt(8, NV.Id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}
